﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ColorReduction
{
	public partial class FrmMain : Form
	{
		private string _fileName;
		private Bitmap _bm;
		private K_Means _km;

		public FrmMain()
		{
			InitializeComponent();

			this.cbxInitialCenters.SelectedIndex = 2;
		}

		private void miOpen_Click(object sender, EventArgs e)
		{
			if (this.odgOpenFile.ShowDialog(this) == DialogResult.OK)
			{
				this.OpenFile(this.odgOpenFile.FileName);
			}
		}

		private void btnProcess_Click(object sender, EventArgs e)
		{
			try
			{
				this.lblTime.Text = "00:00:00.00";
				this.lblTime.Refresh();
				this.btnProcess.Enabled = false;
				Stopwatch stopwch = new Stopwatch();
				stopwch.Start();
				this._km = new K_Means(this._fileName, (int)this.nudKClusters.Value);
				if (!this._km.Init(this.cbxInitialCenters.SelectedIndex, (int)this.nudRadius.Value))
				{
					MessageBox.Show(String.Format("Chưa đủ {0} tâm cụm!", this.nudKClusters.Value));
					this.btnProcess.Enabled = true;
					return;
				}
				TimeSpan ts = stopwch.Elapsed;
// 				this.lblTime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
// 				this.lblTime.Refresh();
				int i = 0;
				bool stop = false;
				while (!stop && i < (int)this.nudMaxIterations.Value)
				{
					this._km.Step();
					i++;
					ts = stopwch.Elapsed;
// 					this.lblTime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
// 					this.lblTime.Refresh();
				}
				stopwch.Stop();
				ts = stopwch.Elapsed;
				this.lblTime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
				this.lblTime.Refresh();
				if (MessageBox.Show("Done", "...", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
				{
					this._km.Show();
				}
				this.btnProcess.Enabled = true;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
				this.btnProcess.Enabled = true;
				return;
			}
		}

		private void miSave_Click(object sender, EventArgs e)
		{
			if (this.sdgSaveFile.ShowDialog(this) == DialogResult.OK)
			{
				this._km.Save(this.sdgSaveFile.FileName);
			}
		}

		private void FrmMain_DragDrop(object sender, DragEventArgs e)
		{
			try
			{
				string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
				string file = files[0];
				this.OpenFile(file);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return;
			}
		}

		private void OpenFile(string fileName)
		{
			this._fileName = fileName;
			this.Text = String.Format("{0} - Color Reduction", this._fileName);
			this._bm = new Bitmap(this._fileName);
			this.pbxSource.Image = _bm;
			this.pbxSource.Refresh();
			this._sbpDimensions.Text = String.Format("Dimensions: {0}x{1}.", this._bm.Width, this._bm.Height);
		}

		private void FrmMain_DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
			else e.Effect = DragDropEffects.None;
		}

		private void miExit_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void miTest_Click(object sender, EventArgs e)
		{
			try
			{
				this._km = new K_Means(this._fileName, (int)this.nudKClusters.Value);
				this._km.ShowGray();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return;
			}
		}
	}
}
